<?php 

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;
use Hash;
use Log;
//20210322
class TaskController extends Controller
{
    //任务列表 adminid role
    public function taskList(Request $request){
    	$info = $request->all();
    	if($info['role']==3){ //普通用户
    		$task_list = Db::table('xfy_task_user')->where(['xfy_task_user.user_id'=>$info['adminid']])->leftJoin('xfy_task', 'xfy_task.taskId', '=', 'xfy_task_user.taskId')->orderByDesc('xfy_task.taskId')->get()->toArray();
    	}else{
    		$task_list = Db::table('xfy_task')->where(['admin_id'=>$info['adminid']])->orderByDesc('taskId')->get()->toArray();
    	}
    	return $task_list;
    }
    
    /**新增任务
     * adminId
     * taskname
     * taskcontent
     * projectId
     * created_at
     * ended_at
    **/
    public function addTask(Request $request){
    	$info = $request->all();
    	$admin_info['admin_id'] = $info['adminId'];
    	$admin_info['taskname'] = $info['taskname'];
    	$admin_info['taskcontent'] = $info['taskcontent'];
    	$admin_info['projectId'] = $info['projectId'];
    	$admin_info['created_at'] = date("Y-m-d H:i:s");
    	$id = Db::table('xfy_task')->insertGetId($admin_info);
    	$user_info['taskId'] = $id;
    	$user_info['created_at'] = date("Y-m-d H:i:s");
    	$user_info['status'] = 0;
	    foreach (json_decode($info['user'],1) as $key => &$value) {
            $user_info['user_id'] = $value;
            $this->sendWx($value,$id,$info['taskcontent'],$info['projectId']);
            Db::table('xfy_task_user')->insert($user_info);
        }
        return 1;
    }
    
    //删除任务
    public function deleteTask(Request $request){
        $info = $request->all();
        // $task = Db::table('xfy_task')->where(['taskId'=>$info['taskid'],'status'=>1])->first();
        Db::table('xfy_task')->where(['taskId'=>$info['taskid'],'status'=>1])->update(['status'=>2]);
        Db::table('xfy_task_user')->where(['taskId'=>$info['taskid'],'status'=>1])->update(['status'=>3]);
        return 1;
    }
    
}